package cn.sfrog.system.service.impl;

import cn.sfrog.common.exception.base.BaseException;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.system.domain.SysAgreementConf;
import cn.sfrog.system.mapper.SysAgreementConfMapper;
import cn.sfrog.system.service.ISysAgreementConfService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * 协议配置Service业务层处理
 */
@Service
public class SysAgreementConfServiceImpl extends ServiceImpl<SysAgreementConfMapper, SysAgreementConf> implements ISysAgreementConfService {
    @Resource
    private SysAgreementConfMapper sysAgreementConfMapper;

    @Override
    public int updateBizAgreementConf(SysAgreementConf bizAgreementConf) {
        LambdaQueryWrapper<SysAgreementConf> wrapper = new LambdaQueryWrapper<SysAgreementConf>();
        wrapper.eq(SysAgreementConf::getContentType,bizAgreementConf.getContentType());
        SysAgreementConf cmsAgreementConf = sysAgreementConfMapper.selectOne(wrapper);

        if(cmsAgreementConf == null){
            //新增
            SysAgreementConf insertConf = new SysAgreementConf();
            insertConf.setContent(bizAgreementConf.getContent());
            insertConf.setContentType(bizAgreementConf.getContentType());
            insertConf.setCreateBy(SecurityUtils.getUsername());
            insertConf.setUpdateBy(SecurityUtils.getUsername());
            insertConf.setCreateTime(new Date());
            insertConf.setUpdateTime(new Date());
            if(sysAgreementConfMapper.insert(insertConf) <= 0){
                throw new BaseException("操作失败");
            }
            return 1;
        }
        //修改
        SysAgreementConf updateConf = new SysAgreementConf();
        updateConf.setId(cmsAgreementConf.getId());
        updateConf.setContent(bizAgreementConf.getContent());
        updateConf.setUpdateTime(new Date());
        if(sysAgreementConfMapper.updateById(updateConf) <= 0){
            throw new BaseException("更新失败");
        }
        return 1;
    }
}
